package com.fengniu.ejb.business.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.business.ICouponBusinessLocal;
import com.fengniu.ejb.business.ICouponBusinessRemote;
import com.fengniu.ejb.dao.CategoryDaoLocal;
import com.fengniu.ejb.dao.ContentDaoLocal;
import com.fengniu.ejb.dao.CouponDaoLocal;
import com.fengniu.ejb.dao.DeltaDaoLocal;
import com.fengniu.ejb.dao.SiteDaoLocal;
import com.fengniu.ejb.dao.UserDaoLocal;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.CouponItem;
import com.fengniu.ejb.entity.Site2Content;
import com.fengniu.ejb.entity.User;
import com.tianbing.adlottery.global.IConstants;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.CouponBusinessLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.CouponBusinessRemoteJndiName)
public class CouponBusinessImpl implements ICouponBusinessLocal, ICouponBusinessRemote {
	@EJB(name = IEJBConstants.UserDaoLocalJndiName)
	private UserDaoLocal userDao;

	@EJB(name = IEJBConstants.SiteDaoLocalJndiName)
	private SiteDaoLocal siteDao;

	@EJB(name = IEJBConstants.ContentDaoLocalJndiName)
	private ContentDaoLocal contentDao;

	@EJB(name = IEJBConstants.CouponDaoLocalJndiName)
	private CouponDaoLocal couponDao;

	@EJB(name = IEJBConstants.CategoryDaoLocalJndiName)
	private CategoryDaoLocal categoryDao;

	@EJB(name = IEJBConstants.DeltaDaoLocalJndiName)
	private DeltaDaoLocal deltaDao;

	public Map<String, Object> getCoupons(long siteSysId, int pgno, int pgsize) {
		Map<String, Object> result = new HashMap<String, Object>();
		Integer[] relationIds = { IConstants.RelationID.S2C_IS_OWNER_OF.getId() };
		Integer[] contentTypeIds = { IConstants.Type.CONTENT_COUPON.getId() };
		List<Site2Content> store2ContentList = siteDao.getListSite2ContentBy(siteSysId, relationIds, -1, null,
				contentTypeIds, null, null, pgno, pgsize, null);
		Long totalCount = siteDao.getCountSite2ContentBy(siteSysId, relationIds, -1, null, contentTypeIds, null, null);
		result.put("site2ContentList", store2ContentList);
		result.put("totalCount", totalCount);
		return result;
	}

	public Site2Content findCoupon(long contentSysId, long storeSiteSysId) {
		Site2Content result = null;
		Integer[] relationIds = { IConstants.RelationID.S2C_IS_OWNER_OF.getId() };
		Integer[] contentTypeIds = { IConstants.Type.CONTENT_COUPON.getId() };
		List<Site2Content> s2cs = siteDao.getListSite2ContentBy(storeSiteSysId, relationIds, contentSysId, null,
				contentTypeIds, null, null, 0, 1, null);
		if (s2cs != null && s2cs.size() > 0) {
			result = s2cs.get(0);
		}
		return result;
	}

	public Map<String, Object> getCouponItems(long userSysId, long storeSysId, long couponSysId, long couponIssueId,
			int pgno, int pgsize) {
		Map<String, Object> result = new HashMap<String, Object>();
		Site2Content store2Coupon = this.findCoupon(couponSysId, storeSysId);
		Integer[] store2CouponRelationIds = { IConstants.RelationID.S2C_IS_OWNER_OF.getId() };
		Integer[] couponTypeIds = { IConstants.Type.CONTENT_COUPON.getId() };

		if (store2Coupon != null) {
			List<CouponItem> couponItemList = couponDao.getListCouponItem(userSysId, storeSysId,
					store2CouponRelationIds, couponSysId, couponTypeIds, couponIssueId, pgno, pgsize);
			Long totalCount = couponDao.getCountCouponItem(userSysId, storeSysId, store2CouponRelationIds, couponSysId,
					couponTypeIds, couponIssueId);
			result.put("couponItemList", couponItemList);
			result.put("totalCount", totalCount);
		}
		return result;
	}

	public CouponItem representCouponItem(long storeSysId, long couponSysId, long userSysId, String couponItemId,
			long couponIssueId, String comment) {
		CouponItem ci = null;
		User user = userDao.findUserBySysId(userSysId);
		Site2Content store2Coupon = this.findCoupon(couponSysId, storeSysId);
		if (store2Coupon != null && user != null) {
			ci = couponDao.findCouponItemByItemId(couponItemId);
			if (ci == null) {
				ci = new CouponItem();
				ci.setCoupon(store2Coupon.getContent());
				ci.setCouponItemId(couponItemId);
				ci.setShortDesc(comment);
				ci.setUser(user);
				ci.setIssueId(couponIssueId);
				couponDao.saveCouponItem(ci);
			}
		}
		return ci;
	}

	public Site2Content findCouponStoreByCouponItemSysId(long couponItemSysId) {
		Site2Content result = null;
		CouponItem ci = couponDao.findCouponItemBySysId(couponItemSysId);
		if (ci != null) {
			result = siteDao.findSite2ContentBy(-1, IConstants.RelationID.S2C_IS_OWNER_OF.getId(), ci.getCoupon()
					.getSysId());
		}
		return result;
	}

	public CouponItem updateCouponItemBonusNumber(long userSysId, CouponItem couponItem) {
		CouponItem result = couponItem;
		if (couponItem != null && couponItem.getUser() != null
				&& couponItem.getUser().getSysId().compareTo(userSysId) == 0) {
			CouponItem oldCouponItem = couponDao.findCouponItemBySysId(couponItem.getSysId());
			if (oldCouponItem.getLotteryNumber0() != null && oldCouponItem.getLotteryNumber0().trim() != ""
					|| oldCouponItem.getLotteryNumber1() != null && oldCouponItem.getLotteryNumber1().trim() != ""
					|| oldCouponItem.getLotteryNumber2() != null && oldCouponItem.getLotteryNumber2().trim() != ""
					|| oldCouponItem.getLotteryNumber3() != null && oldCouponItem.getLotteryNumber3().trim() != ""
					|| oldCouponItem.getLotteryNumber4() != null && oldCouponItem.getLotteryNumber4().trim() != ""
					|| oldCouponItem.getLotteryNumber5() != null && oldCouponItem.getLotteryNumber5().trim() != ""
					|| oldCouponItem.getLotteryNumber6() != null && oldCouponItem.getLotteryNumber6().trim() != ""
					|| oldCouponItem.getLotteryNumber7() != null && oldCouponItem.getLotteryNumber7().trim() != ""
					|| oldCouponItem.getLotteryNumber8() != null && oldCouponItem.getLotteryNumber8().trim() != ""
					|| oldCouponItem.getLotteryNumber9() != null && oldCouponItem.getLotteryNumber9().trim() != "") {
				// nothing to do
				result = oldCouponItem;
			} else {
				couponDao.updateCouponItemLotteryNumbers(couponItem);
			}
		}
		return result;
	}
}
